package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.common.ui.BaseActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.params.ResetPasswordParams;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.Toasts;

import butterknife.Bind;

/**
 * 重置密码
 */
public class ResetPasswordActivity extends BaseActivity {

    private static final String EXTRA_PARAMS = "PARAMS";
    public static void launch(Context context, ResetPasswordParams params) {
        context.startActivity(
                new Intent(context, ResetPasswordActivity.class)
                        .putExtra(EXTRA_PARAMS, params)
        );
    }

    @Bind(R.id.reset_password_hint_text) TextView mResetPasswordHintText;
    @Bind(R.id.new_password_edit) EditText mNewPasswordEdit;
    @Bind(R.id.reset_password_button) Button mResetPasswordButton;

    @Override
    protected int getBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_reset_password;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_reset_password);

        ResetPasswordParams params = (ResetPasswordParams) getIntent()
                .getSerializableExtra(EXTRA_PARAMS);

        // 提示
        mResetPasswordHintText.setText(Html.fromHtml(
                String.format(getString(R.string.text_reset_password_hint), params.getMobile())));

        // 重置密码
        mResetPasswordButton.setOnClickListener(v -> requestResetPassword(params));
    }

    private void requestResetPassword(ResetPasswordParams params){
        String password = mNewPasswordEdit.getText().toString();
        if (TextUtils.isEmpty(password)){
            Toasts.warning(R.string.message_please_enter_your_password);
            return;
        }

        params.setPassword(password);
        DataRepository.get().alterPassword(params, result -> {
            if (result.isSuccess()) {
                Toasts.success(R.string.message_reset_password_success);
                startActivity(new Intent(this, LoginActivity.class));
            } else {
                Toasts.failure(result.getMsg());
            }
        });
    }
}
